/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Parcourir.java
 *
 * Created on 9 nov. 2011, 10:35:16
 */
package org.lestr.otaf.ihm;

import java.awt.Component;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import org.lestr.genver.data.map.ListenedMap;
import org.lestr.genver.data.map.MapListener;

/**
 *
 * @author pibonnin
 */
public class Parcourir extends javax.swing.JPanel {


    public boolean ouvrir = true;


    public ListenedMap<String, Object> variables;


    public String variableCible;


    /** Creates new form Parcourir */
    public Parcourir(ListenedMap<String, Object> variables) {

        initComponents();
        this.variables = variables;
        variables.getListeners().add(new MapListener<String, Object>() {


            @Override
            public void elementPut(String key,
                                   Object oldValue) {

                if (key.equals(variableCible)
                    && !Parcourir.this.variables.get(key).equals(champ.getText()))
                    champ.setText(Parcourir.this.variables.get(key).toString());

            }


            @Override
            public void elementRemoving(String key) {

                if (key.equals(variableCible))
                    champ.setText("");

            }


        });

    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        champ = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();

        champ.setEditable(false);

        jButton1.setText("Parcourir...");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(champ, javax.swing.GroupLayout.DEFAULT_SIZE, 287, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(champ, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        parcourir(this);

    }//GEN-LAST:event_jButton1ActionPerformed


    public boolean parcourir(Component parent) {

        JFileChooser dialogue = new JFileChooser();

        int rslt;

        if (ouvrir)
            rslt = dialogue.showOpenDialog(parent);
        else
            rslt = dialogue.showSaveDialog(parent);

        if (rslt == JFileChooser.APPROVE_OPTION)
            if (!variables.containsKey(variableCible)
                || variables.get(variableCible) == null
                || variables.containsKey(variableCible)
                   && variables.get(variableCible) != null
                   && !variables.get(variableCible).equals(dialogue.getSelectedFile().getAbsolutePath())) {
                variables.put(variableCible, "file:/" + dialogue.getSelectedFile().getAbsolutePath().replaceAll("\\\\", "/"));
                champ.setText(variables.get(variableCible).toString());
            }
        
        return (rslt == JFileChooser.APPROVE_OPTION);
        
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    public javax.swing.JTextField champ;
    private javax.swing.JButton jButton1;
    // End of variables declaration//GEN-END:variables
}

